//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    head_swiper:[],
    notice:[],
    xinshou:{},
    hotrec:[],
    bond:[],
    anima:''
  },
  onReady: function(){
    
    this.animation = wx.createAnimation({
      duration: 3000,
      timingFunction: "ease",
    })
  },
  onLoad: function () {
    this.setIndexData();
  },
  previewImage:function(){
    console.log()
    wx.previewImage({ 
      current: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1147399691,3498367068&fm=27&gp=0.jpg', // 当前显示图片的http链接         
      urls: ['https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1147399691,3498367068&fm=27&gp=0.jpg'] // 需要预览的图片http链接列表       
    })      

  },
  translate: function (e) {
    console.log(e);
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        _this.animation.translate(40, res.windowHeight-200).step()
        _this.setData({ anima: _this.animation.export() })
      },
    })

  },
  setIndexData:function(){
    let _this = this;
    wx.showLoading({
      title: '请稍等...',
    });
    wx.getStorage({
      key: 'index_data',
      success: function (res) {
        if(res.data){
          res.data.borrow_11.borrow_info.map(function (t, i) {
            t.account = (Math.floor(t.account) / 10000).toFixed(2);
            switch (t.borrow_style) {
              case '0':
                return t.borrow_style = "按月等额本息";
                break;
              case '3':
                return t.borrow_style = "按月付息";
                break;
              case '2':
                return t.borrow_style = "到期还款";
                break;
              default:
                return t.borrow_style = "到期还款";
                break;
            }
          });

          res.data.borrow_20.borrow_info.map(function(t,i){
            t.borrow_account_wait = t.borrow_account_wait > 10000 ? (Math.floor(t.borrow_account_wait) / 10000).toFixed(2) + "万" : t.borrow_account_wait;
            t.borrow_account_scale = t.borrow_account_scale == 99.9 ? 100 : (t.borrow_account_scale == 0.00 ? 0.01 : t.borrow_account_scale);
            t.account = parseInt(t.account) > 10000 ? parseInt(t.account) / 10000 : t.account;

            switch (t.borrow_style) {
              case '0':
                return t.borrow_style = "按月等额本息";
                break;
              case '3':
                return t.borrow_style = "按月付息";
                break;
              case '2':
                return t.borrow_style = "到期还款";
                break;
              default:
                return t.borrow_style = "到期还款";
                break;
            }
          })

          res.data.borrow_10.borrow_info.borrow_apr = parseFloat(res.data.borrow_10.borrow_info.borrow_apr);

          _this.setData({
            head_swiper: res.data.banner,
            notice: res.data.affiche,
            xinshou: res.data.borrow_10.borrow_info,
            hotrec: res.data.borrow_20.borrow_info,
            bond: res.data.borrow_11.borrow_info
          });

        } else {
          _this.getData();
        }

        console.log(res.data)
      },
      fail:function(){
        _this.getData();
      },
      complete:function(e){
        wx.hideLoading();
      }
    })
  },
  getData:function(){
    let _this = this;
    wx.request({
      url: 'https://apiv2.dingxindai.com/AppApi/AppGetInfoN.php',
      data: { act: 'home' },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        _this.setData({
          head_swiper: res.data.banner,
          notice: res.data.affiche
        });
        wx.setStorage({
          key: "index_data",
          data: res.data
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  }
});
